<?php

namespace app\dao\shop;

use app\dao\BaseDao;
use app\model\Shop;
use app\model\ShopFreightSpecial;

class FreightSpecialDao extends BaseDao
{
    public function create(array $data)
    {
        return $this->save([
            'freight_id' => $data['freight_id'],
            'min_amount' => $data['min_amount'] * 100,
            'per_amount' => $data['per_amount'] * 100,
            'min_unit' => $data['min_unit'],
            'per_unit' => $data['per_unit'],
            'province' => $data['province'],
            'city' => $data['city'],
            'district' => $data['district'],
        ]);
    }

    public function updateById(array $data, $id)
    {
        $updateList = array();
        if (!empty($data['min_amount']))
        {
            $updateList['min_amount'] = $data['min_amount'] * 100;
        }
        if (!empty($data['per_amount']))
        {
            $updateList['per_amount'] = $data['per_amount'] * 100;
        }
        if (!empty($data['min_unit']))
        {
            $updateList['min_unit'] = $data['min_unit'];
        }
        if (!empty($data['per_unit']))
        {
            $updateList['per_unit'] = $data['per_unit'];
        }
        if (!empty($data['province']))
        {
            $updateList['province'] = $data['province'];
        }
        if (isset($data['city']))
        {  //城市可以为空
            $updateList['city'] = $data['city'];
        }
        if (isset($data['district']))
        {  //镇区可以为空
            $updateList['district'] = $data['district'];
        }

        $this->update($updateList, $id);

        return $this->getById($id);
    }

    public function getById($id)
    {
        return $this->getModel()->where('id', '=', $id)->find();
    }

    protected function setModel(): string
    {
        return ShopFreightSpecial::class;
    }

    public function deleteById($id)
    {
        $this->getModel()->destroy($id);
    }

    public function getByFreightId($id)
    {
        return $this->getModel()->where('freight_id', '=', $id)->select();
    }


}